package com.testcolision.simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;

public class Vehicle extends RigidBody {

	private static final float STEER_LIMIT = 0.75f;
	private static final float STEER_INCREMENT = MathUtils.PI / 32.0f;
	public static final float TYRE_RADIUS = 0.4f;
	public RaycastWheel[] raycastWheels = new RaycastWheel[4];

	public float throttleAmmount = 0;
	public float brakeAmmount = 0;
	public float steerAngle = 0;

	public Vehicle(Vector3 halfsize, float mass) {
		super(halfsize, mass);
		// TODO Auto-generated constructor stub
		for (int i = 0; i < raycastWheels.length; i++) {
			raycastWheels[i] = new RaycastWheel(TYRE_RADIUS);
		}
	}

	public void setThrottle(int throttle) {
		if (throttle == -1 && throttleAmmount >= -20) {
			throttleAmmount -= 2;
		} else if (throttle == 1 && throttleAmmount <= 20) {
			throttleAmmount += 2;
		}
		else if(throttle ==0)
		{
			throttleAmmount = 0;
		}

		raycastWheels[2].wheelTorque += throttleAmmount;
		raycastWheels[3].wheelTorque += throttleAmmount;
		
	}

	public void setBrakes(int brake) {
		if (brake == 1) {
			brakeAmmount = 20;
			throttleAmmount = 0;
		} else {
			brakeAmmount = 0;
		}
		raycastWheels[2].wheelTorque -= raycastWheels[2].wheelSpeed * brakeAmmount * brake;
		raycastWheels[3].wheelTorque -= raycastWheels[3].wheelSpeed * brakeAmmount * brake;

	}

	public void setSteering(int steering) {
		if (steering == -1 && steerAngle < STEER_LIMIT) {
			steerAngle += STEER_INCREMENT;
		}
		if (steering == 1 && steerAngle > -STEER_LIMIT) {
			steerAngle -= STEER_INCREMENT;
		}
		else if(steering ==0){steerAngle = 0;}
		raycastWheels[0].setSteeringAngle(steerAngle);
		raycastWheels[1].setSteeringAngle(steerAngle);
	}
}
